package com.area.cth.controller;

import com.area.cth.pojo.Collect;
import com.area.cth.common.Result;
import com.area.cth.service.CollectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "收藏相关接口")
@RestController
@RequestMapping("/like")
@CrossOrigin
public class CollectController {
    @Autowired
    private CollectService ls;

    @Operation(summary = "新增收藏")
    @PostMapping
    public Result addLike(Integer gId) {
        Collect collect = ls.likeIf(gId);
        if (collect == null) {
            ls.addLike(gId);
            return new Result(0,"收藏成功",null);
        } else {
            return Result.error("已收藏！请勿频繁操作！");
        }
    }

    @Operation(summary = "根据商品id查询个人收藏")
    @GetMapping
    public Result<Collect> likeIf(Integer gId) {
        Collect b = ls.likeIf(gId);
        return Result.success(b);
    }

    @Operation(summary = "取消收藏")
    @DeleteMapping
    public Result deleteLike(Integer lId) {
        ls.deleteLike(lId);
        return new Result(0,"已取消",null);
    }

    @Operation(summary = "根据商品id取消收藏")
    @DeleteMapping("/deleteByGoods")
    public Result deleteLikeByGoodsId(Integer gId) {
        ls.deleteLikeByGoodsId(gId);
        return new Result(0,"已取消",null);
    }

    @Operation(summary = "查询个人收藏的商品")
    @GetMapping("/myCollect")
    public Result<List<Collect>> myCollect() {
        List<Collect> myCollect = ls.getMyCollect();
        return Result.success(myCollect);
    }
}
